﻿Public Class F1_Them_Nguoi_Dung

    Dim DSND As New List(Of NguoiDung)

    Private Sub HienThiDS(ByVal DS As List(Of NguoiDung))
        Dim i As Integer
        For i = 0 To DS.Count - 1
            Dim dong As New ListViewItem()
            dong.Tag = DS(i)
            dong.Text = DS(i).HoTen
            dong.SubItems.Add(DS(i).Username)
            If DS(i).LoaiND = 1 Then
                dong.SubItems.Add("Quản lý")
            Else
                dong.SubItems.Add("")
            End If

            lvdsnd.Items.Add(dong)
        Next
    End Sub

    Private Sub F1_Them_Nguoi_Dung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DSND = XL_NguoiDung.LayDS()
        lvdsnd.Items.Clear()
        HienThiDS(DSND)
    End Sub


    Private Sub lvdsnd_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvdsnd.SelectedIndexChanged
        If (lvdsnd.SelectedItems.Count > 0) Then
            tbxhoten.Text = lvdsnd.SelectedItems.Item(0).Text
            txttaikhoan.Text = lvdsnd.SelectedItems.Item(0).SubItems(1).Text
            If lvdsnd.SelectedItems.Item(0).SubItems(2).Text = "" Then
                cbxad.Checked = False
            Else
                cbxad.Checked = True
            End If
        End If
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        If (tbxhoten.Text = "" Or txttaikhoan.Text = "") Then
            MessageBox.Show("Chua nhap du thong tin nguoi dung!", "Chu y")
        Else
            If (XL_NguoiDung.KiemTraUsername(txttaikhoan.Text) = 1) Then
                MessageBox.Show("ten dang nhap da ton tai!", "Chu y")
            Else
                Dim p As New NguoiDung()
                p.HoTen = tbxhoten.Text
                p.Username = txttaikhoan.Text
                p.MatKhau = "0"
                If cbxad.Checked = True Then
                    p.LoaiND = 1
                Else
                    p.LoaiND = 0
                End If
                p.TrangThai = 1

                Dim kq As Boolean = XL_NguoiDung.Them(p)
                If kq = True Then
                    DSND.Clear()
                    DSND = XL_NguoiDung.LayDS()
                    lvdsnd.Items.Clear()
                    HienThiDS(DSND)
                    MessageBox.Show("Them thanh cong 1 nguoi dung!")
                Else
                    MessageBox.Show("Them khong thanh cong 1!")
                End If
            End If
        End If

    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        If (lvdsnd.SelectedItems.Count > 0) Then
            If MessageBox.Show("Ban co muon xoa vi tri da chon ?", "Chu y!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Dim ten As String = lvdsnd.SelectedItems(0).SubItems(1).Text
                Dim kq As Boolean = XL_NguoiDung.Xoa(ten)
                If kq = True Then
                    DSND.Clear()
                    DSND = XL_NguoiDung.LayDS()
                    lvdsnd.Items.Clear()
                    HienThiDS(DSND)
                    MessageBox.Show("Xoa thanh cong!")
                Else
                    MessageBox.Show("Xoa khong thanh cong")
                End If

            End If

        Else
            MessageBox.Show("Ban chua chon vi tri xoa!")
        End If
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        If MessageBox.Show("Bạn muốn thoát?", "Thông báo!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If

    End Sub
End Class